package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.UserDeductionBo;
import com.joker.orange.domain.vo.UserDeductionVo;
import com.joker.orange.domain.UserDeduction;
import com.joker.orange.mapper.UserDeductionMapper;
import com.joker.orange.service.IUserDeductionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户扣单配置Service业务层处理
 *
 * @author Joker
 * @date 2024-07-15
 */
@RequiredArgsConstructor
@Service
public class UserDeductionServiceImpl implements IUserDeductionService {

    private final UserDeductionMapper baseMapper;

    /**
     * 查询用户扣单配置
     */
    @Override
    public UserDeductionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户扣单配置列表
     */
    @Override
    public TableDataInfo<UserDeductionVo> queryPageList(UserDeductionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserDeduction> lqw = buildQueryWrapper(bo);
        Page<UserDeductionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户扣单配置列表
     */
    @Override
    public List<UserDeductionVo> queryList(UserDeductionBo bo) {
        LambdaQueryWrapper<UserDeduction> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserDeduction> buildQueryWrapper(UserDeductionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserDeduction> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeductionType()), UserDeduction::getDeductionType, bo.getDeductionType());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderCount()), UserDeduction::getOrderCount, bo.getOrderCount());
        return lqw;
    }

    /**
     * 新增用户扣单配置
     */
    @Override
    public Boolean insertByBo(UserDeductionBo bo) {
        UserDeduction add = BeanUtil.toBean(bo, UserDeduction.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户扣单配置
     */
    @Override
    public Boolean updateByBo(UserDeductionBo bo) {
        UserDeduction update = BeanUtil.toBean(bo, UserDeduction.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserDeduction entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户扣单配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
